//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest
//
//import com.intellij.collaboration.async.launchNow
//import com.intellij.collaboration.async.nestedDisposable
//import com.intellij.collaboration.ui.codereview.list.ReviewListViewModel
//import com.intellij.openapi.components.service
//import com.intellij.openapi.project.Project
//import com.intellij.openapi.util.NlsSafe
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.ui.CollectionListModel
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.channels.Channel
//import kotlinx.coroutines.flow.*
//import org.jetbrains.annotations.ApiStatus
//import com.linqingying.gitcode.api.data.GCUser
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestShort
//import com.linqingying.gitcode.authentication.accounts.GitCodeAccount
//import com.linqingying.gitcode.pullrequest.data.GCListLoader
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContext
//import com.linqingying.gitcode.pullrequest.data.service.GCPRPersistentInteractionState
//import com.linqingying.gitcode.pullrequest.ui.filters.GCPRListPersistentSearchHistory
//import com.linqingying.gitcode.pullrequest.ui.filters.GCPRSearchHistoryModel
//import com.linqingying.gitcode.pullrequest.ui.filters.GCPRSearchPanelViewModel
//import com.linqingying.gitcode.ui.avatars.GCAvatarIconsProvider
//import com.linqingying.gitcode.ui.cloneDialog.GCCloneDialogExtensionComponentBase.Companion.items
//import javax.swing.ListModel
//
//@ApiStatus.Experimental
//class GCPRListViewModel internal constructor(
//  project: Project,
//  parentCs: CoroutineScope,
//  private val dataContext: GCPRDataContext
//) : ReviewListViewModel {
//  private val cs = parentCs.childScope()
//
//  private val interactionStateService = project.service<GCPRPersistentInteractionState>()
//  private val repositoryDataService = dataContext.repositoryDataService
//  private val listLoader = dataContext.listLoader
//
//  val account: GitCodeAccount = dataContext.securityService.account
//  private val currentUser: GCUser = dataContext.securityService.currentUser
//  val repository: @NlsSafe String = repositoryDataService.repositoryCoordinates.repositoryPath.repository
//
//  val listModel: ListModel<GCPullRequestShort> = CollectionListModel(listLoader.loadedData).also { model ->
//    listLoader.addDataListener(cs.nestedDisposable(), object : GCListLoader.ListDataListener {
//      override fun onDataAdded(startIdx: Int) {
//        val loadedData = listLoader.loadedData
//        model.add(loadedData.subList(startIdx, loadedData.size))
//      }
//
//      override fun onDataUpdated(idx: Int) {
//        model.setElementAt(listLoader.loadedData[idx], idx)
//      }
//      override fun onDataRemoved(idx: Int) {
//        model.remove(idx)
//      }
//
//      override fun onAllDataRemoved() {
//        model.removeAll()
//      }
//    })
//  }
//
//  private val loadingState = MutableStateFlow(false)
//  val loading: SharedFlow<Boolean> = loadingState.asSharedFlow()
//  private val errorState = MutableStateFlow<Throwable?>(null)
//  val error: SharedFlow<Throwable?> = errorState.asSharedFlow()
//  private val outdatedState = MutableStateFlow(false)
//  val outdated: SharedFlow<Boolean> = outdatedState.asSharedFlow()
//
//  private val hasUpdatesState = MutableStateFlow(false)
//  val hasUpdates = hasUpdatesState.asSharedFlow()
//
//  init {
//    val listenersDisposable = cs.nestedDisposable()
//    listLoader.addLoadingStateChangeListener(listenersDisposable) {
//      loadingState.value = listLoader.loading
//
//      // If transitioning from loading to not loading, check for no updates
//      if (!listLoader.loading) {
//        hasUpdatesState.value = listModel.items.any { !interactionStateService.isSeen(it, currentUser) }
//      }
//    }
//    listLoader.addErrorChangeListener(listenersDisposable) {
//      errorState.value = listLoader.error
//    }
//    dataContext.listUpdatesChecker.addOutdatedStateChangeListener(listenersDisposable) {
//      outdatedState.value = dataContext.listUpdatesChecker.outdated
//    }
//
//    listLoader.addDataListener(listenersDisposable, object : GCListLoader.ListDataListener {
//      override fun onAllDataRemoved() {
//        listLoader.loadMore()
//      }
//    })
//  }
//
//  private val searchHistoryModel = GCPRSearchHistoryModel(project.service<GCPRListPersistentSearchHistory>())
//  val searchVm = GCPRSearchPanelViewModel(cs, project, repositoryDataService, searchHistoryModel, dataContext.securityService.currentUser)
//
//  private val _focusRequests = Channel<Unit>(1)
//  internal val focusRequests: Flow<Unit> = _focusRequests.receiveAsFlow()
//
//  val avatarIconsProvider: GCAvatarIconsProvider = dataContext.avatarIconsProvider
//
//  init {
//    cs.launchNow {
//      searchVm.searchState.collectLatest {
//        listLoader.searchQuery = it.toQuery()
//      }
//    }
//  }
//
//  override fun refresh() {
//    listLoader.reset()
//    repositoryDataService.resetData()
//  }
//
//  fun requestMore() {
//    if (listLoader.canLoadMore()) {
//      listLoader.loadMore()
//    }
//  }
//
//  fun requestFocus() {
//    _focusRequests.trySend(Unit)
//  }
//}
